<template>
  <el-dialog :visible.sync="dialog" :title="is_edit ? '编辑角色':'新增角色'" append-to-body width="500px">
    <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
      <el-form-item label="角色名称" prop="name">
        <el-input v-model="form.name" style="width: 370px;" maxlength="10" />
      </el-form-item>
      <el-form-item label="角色级别" prop="sort">
        <el-input-number
          v-model.number="form.level"
          :min="1"
          :max="100"
          controls-position="right"
          style="width: 370px;"
        />
      </el-form-item>
      <el-form-item label="数据权限">
        <el-select
          v-model="form.dataScope"
          style="width: 370px"
          placeholder="请选择数据权限"
          @change="changeScope"
        >
          <el-option v-for="item in dateScopes" :key="item" :label="item" :value="item" />
        </el-select>
      </el-form-item>
      <el-form-item v-if="form.dataScope === '自定义'" label="数据权限">
        <treeselect
          v-model="deptIds"
          :options="depts"
          multiple
          style="width: 370px"
          placeholder="请选择"
        />
      </el-form-item>
      <el-form-item label="描述信息">
        <el-input v-model="form.remark" style="width: 370px;" rows="5" type="textarea" maxlength="30" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button :loading="loading" type="primary" @click="doSubmit">确认</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { _deptList ,_rolesCreate,_rolesUpdate} from '@/http/ajax'
// import { add, edit } from '@/api/role'
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
export default {
  components: { Treeselect },
  props: {
    is_edit: {
      type: Boolean,
      required: true
    },
    cur_this: {
      type: Object,
      required: null
    }
  },
  data() {
    return {
      dateScopes: ["全部", "本级", "自定义"],
      loading: false,
      dialog: false,
      depts: [],
      deptIds: [],
      form: { name: "", depts: [], remark: "", dataScope: "本级", level: 3 },
      rules: {
        name: [{ required: true, message: "请输入角色名称", trigger: "blur" }]
      }
    };
  },
  methods: {
    domInit() {this.getDepts()},
    cancel() {
      this.resetForm();
    },
    doSubmit() {
      if (this.form.dataScope === "自定义" && this.deptIds.length === 0) {
        this.$message({
          message: "自定义数据权限不能为空",
          type: "warning"
        });
      } else {
        this.form.depts = [];
        if (this.form.dataScope === "自定义") {
          for (let i = 0; i < this.deptIds.length; i++) {
            this.form.depts.push({
              id: this.deptIds[i]
            });
          }
        }
        this.$refs["form"].validate(valid => {
          if (valid) {
            this.loading = true;
            if (this.is_edit) {
              this.doEdit();
            } else this.doAdd();
          } else {
            return false;
          }
        });
      }
    },
    doAdd() {
      _rolesCreate(this.form,res => {
          this.resetForm();
          this.$notify({
            title: "添加成功",
            type: "success",
            duration: 2500
          });
          this.loading = false;
          this.cur_this.getList();
        },err => {
          this.loading = false;
          console.log(err.response.data.message);
        })
    },
    doEdit() {
      _rolesUpdate(this.form,res => {
          this.resetForm();
          this.$notify({
            title: "修改成功",
            type: "success",
            duration: 2500
          });
          this.loading = false;
          this.cur_this.getList();
        },err => {
          this.loading = false;
          console.log(err.response.data.message);
        })
    },
    resetForm() {
      this.dialog = false;
      this.$refs["form"].resetFields();
      this.form = {
        name: "",
        depts: [],
        remark: "",
        dataScope: "本级",
        level: 3
      };
    },
    getDepts() {
      _deptList({ enabled: true },res => {
        this.depts = res.content;
      });
    },
    changeScope() {
      if (this.form.dataScope === "自定义") {
        this.getDepts();
      }
    }
  }
};
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
/deep/ .el-input-number .el-input__inner {
  text-align: left;
}
</style>
